<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="xh">
        <el-input
          v-model="queryParams.xh"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学院" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="enabledDeptOptions" :show-count="true" placeholder="请选择所属学院" style="width: 205px;"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xxgl:xsxx:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xxgl:xsxx:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xxgl:xsxx:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xsxxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="学号" align="center" prop="xh" />
      <el-table-column label="年级" align="center" prop="grade" />
      <el-table-column label="所属学院" align="center" prop="deptName"/>
      <el-table-column label="所属专业" align="center" prop="sszy" />
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="关联用户" align="center" prop="nickName" />
      <el-table-column label="照片" align="center" prop="zp" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.zp" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xxgl:xsxx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xxgl:xsxx:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['xxgl:xsxx:view']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息管理-学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" inline>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" :disabled="this.isView" style="width: 240px;">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="学号" prop="xh">
          <el-input v-model="form.xh" placeholder="请输入学号" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入年级" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="所属学院" prop="deptId">
          <treeselect v-model="form.deptId" :options="enabledDeptOptions" :show-count="true" placeholder="请选择所属学院" :disabled="this.isView"  style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="所属专业" prop="sszy">
          <el-input v-model="form.sszy" placeholder="请输入所属专业" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="关联用户" prop="glId">
          <el-select v-model="form.glId" placeholder="请选择关联用户" filterable clearable :disabled="this.isView" style="width: 240px;">
            <el-option
              v-for="item in userSelectData"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
              <span style="float: left">{{ item.nickName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phoneNumber }}</span></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片" prop="zp">
          <image-preview :src="form.zp" width="147px" height="147px" v-if="this.isView"/>
          <image-upload v-model="form.zp" :limit="1" v-else/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="this.isView" style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="附件" prop="fj">
          <file-upload v-model="form.fj" :disabled="this.isView" :limit="10" style="width: 600px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"  v-if="!this.isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listXsxx, getXsxx, delXsxx, addXsxx, updateXsxx } from "@/api/xxgl/xsxx"
import {deptTreeSelectData, getUserSelect} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
  name: "Xsxx",
  components: {Treeselect},
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息管理-学生信息表格数据
      xsxxList: [],
      // 用户下拉框数据
      userSelectData: [],
      // 弹出层标题
      title: "",
      // 当前用户ID
      currentUserId: this.$store.state.user.id,
      // 当前用户角色
      currentUserRole: this.$store.state.user.roles[0],
      // 所有部门树选项
      deptOptions: undefined,
      // 过滤掉已禁用部门树选项
      enabledDeptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 是否禁用查看按钮
      isView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        xh: null,
        deptId: null,
        phone: null,
        glId: null,
        teacherId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        xh: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属学院不能为空", trigger: "change" }
        ],
        sszy: [
          { required: true, message: "所属专业不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        zp: [
          { required: true, message: "照片不能为空", trigger: "blur" }
        ],
        glId: [
          { required: true, message: "绑定用户不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
    this.getUserSelectData()
  },
  methods: {
    /** 查询信息管理-学生信息列表 */
    getList() {
      this.loading = true
      if(this.currentUserRole === 'student'){
        this.queryParams.glId = this.currentUserId
      }else if(this.currentUserRole === 'teacher'){
        this.queryParams.teacherId = this.currentUserId
      }
      listXsxx(this.queryParams).then(response => {
        this.xsxxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        name: null,
        sex: null,
        age: null,
        xh: null,
        grade: null,
        deptId: null,
        sszy: null,
        phone: null,
        email: null,
        zp: null,
        fj: null,
        glId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelectData().then(response => {
        this.deptOptions = response.data
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)))
      })
    },
    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter(dept => {
        if (dept.disabled) {
          return false
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children)
        }
        return true
      })
    },
    /** 查询用户下拉框数据 */
    getUserSelectData() {
      /* 2:学生   3:老师”*/
      const roleId = 2
      getUserSelect(roleId).then(response => {
        this.userSelectData = response.data
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.isView = false
      this.open = true
      this.title = "添加"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const studentId = row.studentId || this.ids
      getXsxx(studentId).then(response => {
        this.form = response.data
        this.isView = false
        this.open = true
        this.title = "修改"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset()
      const studentId = row.studentId || this.ids
      getXsxx(studentId).then(response => {
        this.form = response.data
        this.open = true
        this.isView = true
        this.title = "查看"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studentId != null) {
            updateXsxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addXsxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.studentId || this.ids
      this.$modal.confirm('是否确认删除?').then(function() {
        return delXsxx(studentIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xxgl/xsxx/export', {
        ...this.queryParams
      }, `学生信息${this.$dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`)
    }
  }
}
</script>
